/**
 * 
 */
package com.mole.rbac.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.mole.rbac.dao.CourseDao;
import com.mole.rbac.entity.Course;

/**
   *   说明：
 * @author maxin
 * @version 2019年9月28日上午2:10:28
 */
@Repository
public class CourseDaoImpl implements CourseDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public int insert(Course course) {
		String sql = "insert into t_course(c_name, c_hour, c_teacher) values(?, ?, ?)";
		try {
			return jdbcTemplate.update(sql, course.getC_name(), course.getC_hour(), course.getC_teacher());
		} catch(DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public int update(Course course) {
		String sql = "update t_course set c_name = ?, c_hour = ?, c_teacher = ? where c_id = ?";
		try {
			return jdbcTemplate.update(sql, course.getC_name(), course.getC_hour(), course.getC_teacher(), course.getC_id());
		} catch(DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public int deleteById(Long id) {
		String sql = "delete from t_course where c_id= ?";
		try {
			return jdbcTemplate.update(sql, id);
		} catch(DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public Course selectById(Long id) {
		String sql = "select * from t_course where c_id = ?";
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = jdbcTemplate.queryForMap(sql, new Object[]{id});
			String name = map.get("c_name").toString();
			int hour = Integer.parseInt(map.get("c_hour").toString());
			String teacher = map.get("c_teacher").toString();
			return new Course(id, name, hour, teacher);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<Course> selectAll() {
		String sql = "select * from t_course";
		List<Course> listCourse = new ArrayList<Course>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = jdbcTemplate.queryForList(sql);
			
			for(Map<String, Object> item : list) {
				long id = Long.parseLong(item.get("c_id").toString());
				String name = item.get("c_name").toString();
				int hour = Integer.parseInt(item.get("c_hour").toString());
				String teacher = item.get("c_teacher").toString();
				Course Course = new Course(id, name, hour, teacher);
				listCourse.add(Course);
			}
			return listCourse;
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<Course> selectByPage(int pageIndex, int pageSize) {
		if(pageIndex<=0) {
			pageIndex = 0;
		} else {
			pageIndex = pageIndex -1;
		}
		String sql = "select * from t_course limit ?,?";
		List<Course> listCourse = new ArrayList<Course>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = jdbcTemplate.queryForList(sql, new Object[] {pageIndex, pageSize});
			for(Map<String, Object> item : list) {
				long id = Long.parseLong(item.get("c_id").toString());
				String name = item.get("c_name").toString();
				int hour = Integer.parseInt(item.get("c_hour").toString());
				String teacher = item.get("c_teacher").toString();
				Course course = new Course(id, name, hour, teacher);
				listCourse.add(course);
			}
			return listCourse;
		} catch(DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
	
}
